﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Telerik.WinControls;
using Telerik.WinControls.UI;

using LIM.controller;
using LIM.model;

namespace LIM.forms
{
    public class BaseRadForm : Telerik.WinControls.UI.RadForm
    {
        public List<object> ControlPermissionList = new List<object>();
        
        protected override void OnLoad(EventArgs e)
        {
            
            if (InitUserInfos.IsAdmin) // check for user is Admin 
            {
                base.OnLoad(e);
                return;
            }

            //BaseRadForm BaseRadForm = ((BaseRadForm)this.ActiveMdiChild);
            FormPermissionsInfo FormPermissionsInfo = InitUserInfos.FormPermissions.Find(f => f.Name == this.Name);
            if (FormPermissionsInfo == null)
            {
                try
                {
                    ApplyNotPermission();
                    return;
                }
                catch { }                
            }
            else
            {
                foreach (var item in this.ControlPermissionList)
                {
                    if (item is RadMenuItem)
                    {
                        RadMenuItem radItem = (RadMenuItem)item;
                        ControlPermissionsInfo ControlPermissionsInfo = FormPermissionsInfo.Controls.Find(c => c.Name == radItem.Name);
                        if (ControlPermissionsInfo != null)
                            radItem.Enabled = ControlPermissionsInfo.IsEnabled;
                        else
                            radItem.Visibility = ElementVisibility.Collapsed;

                    }
                    else if (item is RadButton)
                    {
                        RadButton radItem = (RadButton)item;
                        ControlPermissionsInfo ControlPermissionsInfo = FormPermissionsInfo.Controls.Find(c => c.Name == radItem.Name);
                        if (ControlPermissionsInfo != null)
                            radItem.Enabled = ControlPermissionsInfo.IsEnabled;
                        else
                            radItem.Visible = false;
                    }
                }

            }            
        }

        private void CloseForm()
        {
            this.Close();
        }
        private void ApplyNotPermission()
        {
            RadDesktopAlert_SavingStatus.ShowAlertNotPermission("Màn hình " + this.Text, "Hệ thống từ chối truy cập!");
            // close form
            this.BeginInvoke(new MethodInvoker(CloseForm));
        }
    }
}
